#include "unique_ptr.hpp"
#include <memory>
#include <iostream>

struct Test
{
    Test() : _val(0) { std::cout << "Test()" << std::endl; }
    Test(int val) : _val(val) { std::cout << "Test(int val)" << std::endl; }
    ~Test() { std::cout << "~Test()" << std::endl; }
    int _val;
};
void test1()
{
    using MyUnique_ptr::Unique_ptr;
    Unique_ptr<Test> u_ptr = MyUnique_ptr::make_Unique<Test>();
    Unique_ptr<Test> u_ptr1 = MyUnique_ptr::make_Unique<Test>(12);
    std::cout << u_ptr.get()->_val << std::endl;
    std::cout << (*u_ptr)._val << std::endl;
    std::cout << u_ptr1.get()->_val << std::endl;

    u_ptr.release();
    if (u_ptr.get() == nullptr) // true
        std::cout << "_ptr is nullptr" << std::endl;

    Unique_ptr<Test> u_ptr2 = MyUnique_ptr::make_Unique<Test>(20);
    if (u_ptr2.get() == nullptr) // false
        std::cout << "_ptr is nullptr" << std::endl;
    else
        std::cout << "_ptr is not nullptr" << std::endl;

    u_ptr2.reset(u_ptr.get());
    if (u_ptr2.get() == nullptr) // true
        std::cout << "_ptr is nullptr" << std::endl;

    // 验证operator bool
    if (!u_ptr2)
        std::cout << "_ptr is nullptr" << std::endl;

    // 编译报错，符合unique_ptr的内存管理理念
    // Unique_ptr<Test> u_ptr1(u_ptr);
    // Unique_ptr<Test> u_ptr2 = u_ptr;
}
int main()
{
    // 拷贝构造和赋值重载都是删除的
    // std::unique_ptr<int> auto_pointer(new int);
    // std::unique_ptr<int> ptr1 = auto_pointer;
    test1();
}